﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TournamentDatabase.Objects;
using TournamentDatabase.Database;
using TournamentDatabase.Singletons;

namespace TournamentDatabase.DialogBoxes.Select
{
	/// <summary>
	/// Interaction logic for OrganiserSelectDialogBox.xaml
	/// </summary>
	public partial class OrganiserSelectDialogBox : Window
	{
		Organiser m_selectedOrganiser;
		List<Organiser> m_organiserList;
		string m_currentSearch;

		public List<Organiser> OrganiserList
		{
			get { return m_organiserList; }
			set { m_organiserList = value; }
		}

		public Organiser SelectedOrganiser
		{
			get { return m_selectedOrganiser; }
			set { m_selectedOrganiser = value; }
		}

		public OrganiserSelectDialogBox()
		{
			InitializeComponent();

			Search();

			this.DataContext = this;
		}

		private void selectButton_Click(object sender, RoutedEventArgs e)
		{
			if (m_selectedOrganiser == null)
				return;
			DialogResult = true;
		}

		private void OnSearch(string searchText)
		{
			m_currentSearch = searchText;
			Search();
		}

		private void Search()
		{
			if (m_currentSearch == null || m_currentSearch.Length == 0)
			{
				m_organiserList = DatabaseSingleton.GetDatabaseAccessor().QueryOrganiser("", OrderBy.None, "");
			}
			else
			{
				string where = "name = '" + m_currentSearch + "'";
				m_organiserList = DatabaseSingleton.GetDatabaseAccessor().QueryOrganiser(where, OrderBy.None, "");
			}
		}
	}
}
